<template>
	<view class="d-flex flex-column justify-start px-30 manual-sale">
		<view class="input-box">
			<u-input v-model="code" :border="false" placeholder="请输入智能卡号/票券号/手机号"></u-input>
		</view>
		<view class="sale-btn" @click="$u.throttle(sellNow, 1000)">立即核销</view>
		<u-modal v-model="showResult" :content="resultContent" title="核销失败" :show-cancel-button="false"
			confirm-text="关闭"></u-modal>
	</view>
</template>

<script>
	import {
		// writeOff,
		// writeOffMember
		manualWriteOff
	} from "@/api/home.js"
	export default {
		data() {
			return {
				code: "",
				ticketFlag: true,
				userInfo: {},
				scenicInfo: null,
				showResult: false,
				resultContent: '',
				deptId: ''
			}
		},
		onShow() {
			this.code = ""
			this.userInfo = this.$store.getters.getUserInfo;
			this.scenicInfo = uni.getStorageSync('scenicInfo') || null
			this.deptId = uni.getStorageSync('deptId') || ''
		},
		methods: {
			sellNow() {
				if (this.code == "") {
					uni.showToast({
						title: '请输入智能卡号/票券号/手机号',
						duration: 2000,
						icon: "none"
					})
				} else {
					if (this.userInfo == null) {
						uni.navigateTo({
							url: '/pages/my-sub/login/login'
						})
					} else if (this.scenicInfo == null) {
						uni.showToast({
							title: '请选择值班场地',
							icon: 'none'
						})
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/tabbar/my/my'
							})
						}, 1500)
					} else {
						uni.showLoading({
							title: '核销中...',
							mask: true
						})
						manualWriteOff({
							writeOffNo: this.code,
							scenicSpotId: this.scenicInfo.scenicSpotId,
							deptId: this.deptId
						}).then(res => {
							const data = JSON.stringify(res.data)
							uni.navigateTo({
								url: `../sale-success/sale-success?data=${data}&mark=2`,
							});
						}).catch(err => {
							this.resultContent = err.msg
							this.showResult = true
						}).finally(() => {
							uni.hideLoading()
						})
						
					}
				}
			}
		}
	}
</script>
<style>
	page {
		background: #eee;
	}

	.manual-sale .u-input {
		background: #fff;
		border-radius: 10rpx !important;
	}

	.manual-sale .u-input__input {
		height: 78rpx;
		padding: 0 30rpx;
	}
	.u-input__right-icon {
		padding-right: 16rpx;
	}
</style>

<style lang="scss" scpoed>
	.manual-sale {
		.input-box {
			width: 690rpx;
			box-sizing: border-box;
			padding: 30rpx 0;
			position: relative;
			margin-top: 60rpx;

			image {
				width: 30rpx;
				height: 31rpx;
				position: absolute;
				left: 20rpx;
				top: 39%;
				z-index: 9;
			}
		}

		.sale-btn {
			width: 670rpx;
			height: 90rpx;
			background: #F4C233;
			border-radius: 45rpx;
			font-size: 30rpx;
			text-align: center;
			color: #000;
			line-height: 90rpx;
			margin-top: 126rpx;
		}
	}
</style>